redis.call("SELECT", 2)

local key = ARGV[1]
local rate = "rate"
local interval = "interval"
local type = "type"
local counts = "counts"

local permits = "{" .. key .. "}:permits"
local value = "{" .. key .. "}:value"

if redis.call("HEXISTS", key, rate) == 1 then
    redis.call("HDEL", key, rate)
end
if redis.call("HEXISTS", key, interval) == 1 then
    redis.call("HDEL", key, interval)
end
if redis.call("HEXISTS", key, type) == 1 then
    redis.call("HDEL", key, type)
end

if redis.call("ZCARD", permits) == 1 then
    local permitsList = redis.call("ZRANGE", permits, 0, -1)
    for i = 1, #permitsList do
        redis.call("ZREM", permits, permitsList[i])
    end
end

if redis.call("GET", value) ~= nil then
    redis.call("DEL", value)
end

return true
